package com.example.qjia.model;

import android.os.Handler;
import android.util.Log;

import com.example.qjia.Listener.LoginListener;

import com.example.qjia.basic.MyApplication;
import com.example.qjia.data.romotedata.ResponseData;
import com.example.qjia.data.romotedata.Result;
import com.example.qjia.data.romotedata.Student;
import com.example.qjia.data.romotedata.person;
import com.example.qjia.util.retrofit.RetrofitNet;

import rx.Observer;

/**
 * Created by ying_xiang on 2016/8/10.
 */
public class LoginModelImp implements LoginModel {
    @Override
    public void login(final String studentId, final String password, final LoginListener listener) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                new RetrofitNet().login(studentId,password,new Observer<ResponseData<Student>>() {
                    @Override
                    public void onCompleted() {
                        //完成
                        System.out.println("finish");
                    }

                    @Override
                    public void onError(Throwable e) {
                        //异常
                        System.out.println("error");
                        Log.e("MAIN2",e.getLocalizedMessage()+"--"+e.getMessage());
                    }

                    @Override
                    public void onNext(ResponseData<Student> baseBean) {
                        //成功
                        ResponseData<Student> p=baseBean;
                        if(p.getCode()==0) {            //表示登录成功
                            Student student = p.getData();

                            System.out.println(p.getCode() + "--" + student.toString());
                            MyApplication.setLogin();
                            MyApplication.setStudentMessage(student);//设置登录状态，并且保存用户的信息
                            listener.success();
                        }else{
                            listener.failed(p.getMsg());
                        }

                    }
                });




            }
        }, 1000);





        }

}